package cn.fraudmetrix.octopus.modules.mock.controller;

import cn.fraudmetrix.octopus.modules.mock.dao.H5MockCaseDao;
import cn.fraudmetrix.octopus.modules.mock.entity.H5MockCaseEntity;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author libao.zheng
 * @email libao.zheng@tongdun.cn
 * @date 2018/12/21 09:33
 */
@RestController
@RequestMapping("/mock/h5MockCase")
public class H5MockCaseController {
    @Resource
    private H5MockCaseDao h5MockCaseDao;
    private final Logger LOGGER = LoggerFactory.getLogger(getClass());

    @PostMapping("/query")
    public List<H5MockCaseEntity> query(@RequestBody H5MockCaseEntity h5MockCaseEntity) {
        return h5MockCaseDao.select(h5MockCaseEntity);
    }

    @PostMapping("/add")
    public boolean add(@RequestBody H5MockCaseEntity h5MockCaseEntity) {
        String maxCaseId = h5MockCaseDao.selectMaxCaseId();
        String newCaseId;
        if (StringUtils.isBlank(maxCaseId)) {
            newCaseId = "case_0001";
        } else {
            maxCaseId = maxCaseId.replace("case_", "");
            int num = maxCaseId.lastIndexOf("0");
            int i = Integer.parseInt(maxCaseId.substring(num + 1)) + 1;
            newCaseId = "case_" + String.format("%04d", i);
        }
        h5MockCaseEntity.setH5CaseId(newCaseId);
        return h5MockCaseDao.insert(h5MockCaseEntity);
    }

    @PostMapping("/update")
    public boolean update(@RequestBody H5MockCaseEntity h5MockCaseEntity) {
        return h5MockCaseDao.update(h5MockCaseEntity);
    }

}
